﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Text;

public partial class Admin_Components_Ajax_LoadMenu : System.Web.UI.UserControl
{
    string shtml = "";
    string temp = "";
    string i_UserID;
    DatabaseHelper DB = new DatabaseHelper();
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && Session["isChangePass"] == null)
        {
            loadMenu();
        }
    }
    private void loadMenu()
    {
        if (Session["UserName"] != null && Session["RoleID"] != null)
        {
            DBDataContext db = new DBDataContext();
            List<Function> f = new List<Function>();
            if (Session["RoleID"].ToString() == "1")
            {
                f = (from c in db.Functions
                     where c.Status == ObjectStatus.Active
                     && c.IsVisible == true
                     select c).ToList();
            }
            else
            {
                f = (from a in db.RoleFunctions
                     join b in db.Users on a.RoleID equals b.RoleID
                     join c in db.Functions on a.FunctionID equals c.ID
                     where (b.UserName == Session["UserName"].ToString())
                     && c.IsVisible == true
                     select c
                    ).ToList();
            }
           StringBuilder sb = new StringBuilder();
            sb.Append("<li ><a href='" + ResolveUrl("~/Default.aspx") + "'><span class='glyphicon glyphicon-home'></span>Trang chủ</a></li>");

            List<Function> p0 = f.Where(x => x.ParentID == 0).ToList();
            foreach (var p0i in p0)
            {
                string title = p0i.FunctionName;
                int id = p0i.ID;
                string url = p0i.MethodName;

                sb.Append("<li class='dropdown'>");
                sb.Append("<a class='dropdown-toggle' data-toggle='dropdown' href='" + url + "'>" + title + "</a>");


                List<Function> p1 = f.Where(x => x.ParentID == id).ToList();
                if (p1.Count > 0)
                {
                    sb.Append("<ul class='dropdown-menu'>");
                    foreach (var p1i in p1)
                    {
                        string title1 = p1i.FunctionName;
                        int id1 = p1i.ID;
                        string url1 = p1i.MethodName;


                        List<Function> p2 = f.Where(x => x.ParentID == id1).ToList();
                        if (p2.Count > 0)
                        {
                            sb.Append("<li class='dropdown-nested'><a class='dropdown-toggle' data-toggle='dropdown' href='javascript:void(0)'>" + title1 + "</a>");
                            sb.Append("<ul class='dropdown-menu sub-menu dropdown-submenu'>");
                            foreach (var p2i in p2)
                            {
                                string title2 = p2i.FunctionName;
                                int id2 = p2i.ID;
                                string url2 = ResolveUrl("~/" + p2i.MethodName);

                                sb.Append("<li><a href='" + url2 + "'>" + title2 + "</a></li>");
                            }
                            sb.Append("</ul>");
                        }
                        else
                        {
                            sb.Append("<li><a href='" + url1 + "'>" + title1 + "</a>");
                        }
                        sb.Append("</li>");
                    }
                    sb.Append("</ul>");
                }
                sb.Append("</li>");

            }
            smoothmenu1.InnerHtml = sb.ToString();
        }
    }
    private void loadMenuBK()
    {
        if (Session["UserName"] != null && Session["RoleID"] != null)
        {
            i_UserID = Session["UserName"].ToString();
            int roleId = int.Parse(Session["RoleID"].ToString());
            if (Session["isLogin"] != null)
            {
                if (roleId == 1)        //Quản trị hệ thống
                {
                    //Begin by Nghiemtn
                    DBDataContext db = new DBDataContext();
                    List<Function> f = (from c in db.Functions
                                        where c.Status == ObjectStatus.Active
                                        && c.IsVisible == true
                                        select c).ToList();

                    StringBuilder sb = new StringBuilder();
                    sb.Append("<li ><a href='" + ResolveUrl("~/Default2.aspx") + "'><span class='glyphicon glyphicon-home'></span>Trang chủ</a></li>");

                    List<Function> p0 = f.Where(x => x.ParentID == 0).ToList();
                    foreach (var p0i in p0)
                    {
                        string title = p0i.FunctionName;
                        int id = p0i.ID;
                        string url = p0i.MethodName;

                        sb.Append("<li class='dropdown'>");
                        sb.Append("<a class='dropdown-toggle' data-toggle='dropdown' href='" + url + "'>" + title + "</a>");


                        List<Function> p1 = f.Where(x => x.ParentID == id).ToList();
                        if (p1.Count > 0)
                        {
                            sb.Append("<ul class='dropdown-menu'>");
                            foreach (var p1i in p1)
                            {
                                string title1 = p1i.FunctionName;
                                int id1 = p1i.ID;
                                string url1 = p1i.MethodName;


                                List<Function> p2 = f.Where(x => x.ParentID == id1).ToList();
                                if (p2.Count > 0)
                                {
                                    sb.Append("<li class='dropdown-nested'><a class='dropdown-toggle' data-toggle='dropdown' href='javascript:void(0)'>" + title1 + "</a>");
                                    sb.Append("<ul class='dropdown-menu sub-menu dropdown-submenu'>");
                                    foreach (var p2i in p2)
                                    {
                                        string title2 = p2i.FunctionName;
                                        int id2 = p2i.ID;
                                        string url2 = ResolveUrl("~/" + p2i.MethodName);

                                        sb.Append("<li><a href='" + url2 + "'>" + title2 + "</a></li>");
                                    }
                                    sb.Append("</ul>");
                                }
                                else
                                {
                                    sb.Append("<li><a href='" + url1 + "'>" + title1 + "</a>");
                                }
                                sb.Append("</li>");
                            }
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    smoothmenu1.InnerHtml = sb.ToString();
                    //End by Nghiemtn--
                }
                else
                {
                    DB.ClearParameter();

                    DbParameter paramParentID = DB.Parameter;
                    paramParentID.ParameterName = "ParentID";
                    paramParentID.DbType = DbType.String;
                    paramParentID.Value = 0;

                    DbParameter paramUserID = DB.Parameter;
                    paramUserID.ParameterName = "UserName";
                    paramUserID.DbType = DbType.String;
                    paramUserID.Value = Utils.KillChars(i_UserID.ToString());

                    DbParameter paramType = DB.Parameter;
                    paramType.ParameterName = "Type";
                    paramType.DbType = DbType.String;
                    paramType.Value = FunctionType.Function;

                    DbParameter paramSystemFunction = DB.Parameter;
                    paramSystemFunction.ParameterName = "SystemFunction";
                    paramSystemFunction.DbType = DbType.String;
                    paramSystemFunction.Value = SystemFunction.Inside;

                    DB.AddParameter(paramParentID);
                    DB.AddParameter(paramUserID);
                    DB.AddParameter(paramType);
                    DB.AddParameter(paramSystemFunction);

                    dt = DB.ExecuteDataTable("Function_Permission", CommandType.StoredProcedure, ConnectionState.CloseOnExit);

                    StringBuilder sb = new StringBuilder();
                    sb.Append("<li ><a href='" + ResolveUrl("~/") + "'><span class='glyphicon glyphicon-home'></span>Trang chủ</a></li>");
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            string title = dr["FunctionName"].ToString();
                            string id = dr["ID"].ToString();
                            string url = dr["MethodName"].ToString();

                            //LOAD MAIN MENU ---------------------------------------------------------------------
                            sb.Append("<li class='dropdown'>");
                            sb.Append("<a class='dropdown-toggle' data-toggle='dropdown' href='" + url + "'>" + title + "</a>");
                            sb.Append("<ul class='dropdown-menu'>");
                            DataTable dtL1 = new DataTable();
                            DB.ClearParameter();
                            DB.AddParameter("@ParentID", id);

                            DB.AddParameter("@UserName", Utils.KillChars(i_UserID.ToString()));
                            DB.AddParameter("@Type", FunctionType.Function);
                            DB.AddParameter("@SystemFunction", SystemFunction.Inside);

                            dtL1 = DB.ExecuteDataTable("Function_Permission", CommandType.StoredProcedure, ConnectionState.CloseOnExit);
                            if (dtL1.Rows.Count > 0)
                            {
                                foreach (DataRow drcon in dtL1.Rows)
                                {
                                    string l1Title = drcon["FunctionName"].ToString();
                                    string l1Id = drcon["ID"].ToString();
                                    string l1Url = drcon["MethodName"].ToString();

                                    sb.Append("<li class='dropdown-nested'>");

                                    sb.Append("<a class='dropdown-toggle' data-toggle='dropdown' href='" + l1Url + "'>" + l1Title + "</a>");

                                    var i = drcon["ID"].ToString().Trim();
                                    DB.ClearParameter();
                                    DB.AddParameter("@ParentID", i);
                                    DB.AddParameter("@UserName", Utils.KillChars(i_UserID.ToString()));
                                    DB.AddParameter("@Type", FunctionType.Function);
                                    DB.AddParameter("@SystemFunction", SystemFunction.Inside);
                                    DataTable dtL2 = DB.ExecuteDataTable("Function_Permission", CommandType.StoredProcedure, ConnectionState.CloseOnExit);

                                    if (dtL2.Rows.Count > 0)
                                    {
                                        sb.Append("<ul class='dropdown-menu sub-menu'>");
                                        foreach (DataRow m in dtL2.Rows)
                                        {
                                            string l2Title = m["FunctionName"].ToString();
                                            string l2Id = m["ID"].ToString();
                                            string l2Url = m["MethodName"].ToString();
                                            sb.Append("<li><a href='" + l2Url + "'>" + l2Title + "</a></li>");
                                        }
                                        sb.Append("</ul>");
                                    }
                                    sb.Append("</li>");
                                }
                            }

                            sb.Append("</ul>");
                            sb.Append("</li>");
                        }
                        smoothmenu1.InnerHtml = sb.ToString();
                    }
                }

            }
        }
    }
}